package com.unidt.www.module.third.oa.message;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.commons.lang3.StringUtils;

import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author sk
 * @Description:
 * @Date 2022/12/7 16:04
 **/
@Slf4j
public class MsgSendUtil {

    private static  final  String url = "http://220.191.242.88:8080/services/workflowServiceQyrz";

    /**
     * 功能描述: OA消息发送
     * @author sk
     * @date 2022/12/7
     * @param message
     * @return java.lang.Boolean
     */

    public static Boolean send(OaMessageBody message) {

        try {
            EndpointReference targetEPR = new EndpointReference(url);
            RPCServiceClient sender = new RPCServiceClient();
            Options options = sender.getOptions();
            options.setTimeOutInMilliSeconds(2 * 20000L);
            options.setTo(targetEPR);

            QName qname = new QName("weaver.zj.webservice.WorkFlowServiceQyrz", "doCreateWorkflowRequest");
            Map<String, Object> map = new HashMap<>();
            map.put("requestname", message.getBody());
            map.put("requesttype", message.getMsgType());

            map.put("jsr", message.getToUser());

            String param_json = JSON.toJSONString(map);
            log.info("发送消息:{}",param_json);

            Object[] param = new Object[]{param_json};
            Class<?>[] types = new Class[]{String.class};

            Object[] response = sender.invokeBlocking(qname, param, types);
            if (Objects.nonNull(response) || response.length > 0) {
                String responseBody = (String) response[0];
                if(StringUtils.isNotBlank(responseBody)){
                    log.info("返回消息:{}",responseBody);
                    MsgResponse msgResponse =  JSON.parseObject(responseBody,MsgResponse.class);
                    if(msgResponse.getResult().equals(1)){
                        return  true;
                    }
                }


            }
        } catch (Exception e) {
            log.error("发送OA消息失败", e);
        }

        return false;
    }

    public static void main(String[] args) {
        OaMessageBody messageBody = new OaMessageBody("1309748", "工作流程引擎平台消息测试 ", 1);
        send(messageBody);
    }
}
